package com.oyjd.ybx.business.msg.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.oyjd.ybx.bean.po.Information;
import com.oyjd.ybx.bean.vo.InformationVO;
import com.oyjd.ybx.fw.action.Page;
import com.oyjd.ybx.fw.dao.BaseDao;

/**
 * 
 * 资讯信息数据处理层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class MsgDao extends BaseDao {
	/**
	 * 获取资讯信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<InformationVO> getList(Page<InformationVO> page) {
		String hqlResult = "select new " + InformationVO.class.getName() + InformationVO.STRUCT;
		String hqlCount = "select count(information)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Information.class.getName()).append(" information");
		sb.append(" where 1=1");
		Map<String, String[]> paras = page.getParas();
		List<Object> ps = new ArrayList<Object>();
		for (String key : paras.keySet()) {
			if ("msgTitle".equals(key) || "msgContent".equals(key) || "creator".equals(key)) {
				sb.append(" and information.").append(key).append(" like ?");
				ps.add("%" + paras.get(key)[0] + "%");
			} else if ("classId".equals(key)) {
				sb.append(" and information.").append(key).append(" = ?");
				ps.add(paras.get(key)[0]);
			}
		}
		sb.append(" order by information.orderKey desc");
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}

	/**
	 * 根据ID获取资讯信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public InformationVO getInformationVO(String msgId) {
		InformationVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(InformationVO.class.getName()).append(InformationVO.STRUCT);
		sb.append(" from ").append(Information.class.getName()).append(" information");
		sb.append(" where information.msgId=?");
		Query q = this.getSession().createQuery(sb.toString());
		q.setParameter(0, msgId);
		List<InformationVO> tmpList = (List<InformationVO>) q.list();
		if (tmpList.size() > 0) {
			result = tmpList.get(0);
		}
		return result;
	}

	/**
	 * 删除资讯信息
	 * 
	 * @param ids
	 *            多个ID
	 * @return
	 */
	public void remove(String[] ids) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from ").append(Information.class.getName()).append(" information where");
		for (int i = 0; i < ids.length; i++) {
			sb.append(0 == i ? "" : " or");
			sb.append(" information.msgId=?");
		}
		Query q = getSession().createQuery(sb.toString());
		for (int i = 0; i < ids.length; i++) {
			q.setParameter(i, ids[i]);
		}
		q.executeUpdate();
	}

	/**
	 * 获取前端大类商品信息展示列表
	 * 
	 * @param classId
	 *            大类类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<InformationVO> getFrontList(String classId, int size) {
		String hqlResult = "select new " + InformationVO.class.getName() + InformationVO.STRUCT;
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(Information.class.getName()).append(" information");
		sb.append(" where information.classId=?");
		sb.append(" order by information.orderKey desc");

		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		q.setParameter(0, classId);
		q.setFirstResult(0);
		q.setMaxResults(size);
		return (List<InformationVO>) q.list();
	}

}
